﻿using AutoMapper;
using MediatR;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.Api.Dto;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class ListCompaniesHandler : IRequestHandler<ListCompaniesCommand, ApiResult<List<CompaniesDto>>>
    {
        public ListCompaniesHandler(IBaseRepository<CompaniesModel> companRepository,IMapper mapper)
        {
            CompanRepository = companRepository;
            Mapper = mapper;
        }

        public IBaseRepository<CompaniesModel> CompanRepository { get; }
        public IMapper Mapper { get; }

        /// <summary>
        /// 显示养殖公司信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<CompaniesDto>>> Handle(ListCompaniesCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<CompaniesDto>>();

            var com= CompanRepository.GetAll().Where(x=>x.Id==request.CompaniesId);
            

            List<CompaniesDto>? mapper=Mapper.Map<List<CompaniesDto>>(com);

            res.Message = "显示公司成功";
            res.Code = EnumCode.Success;
            res.Data =mapper;
            return res;
        }
    }
}
